package com.penghaisoft.microwms.material.web.insidewarehouse.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.wms.model.RawLockLog;
import com.penghaisoft.microwms.common.wms.model.RawStorageSn;
import com.penghaisoft.microwms.material.web.insidewarehouse.service.IMaterialInsideService;

import lombok.extern.slf4j.Slf4j;

import com.penghaisoft.microwms.common.controller.BaseController;


/** 
* @ClassName: InsideRawStorageSnController 
* @Description: 原材料库存明细查询
* @author jzh
* @date 2018年5月18日 上午11:10:10  
*/
@RestController
@RequestMapping(value = "insidewarehouse/RawStorageSn")
@Slf4j
public class InsideRawStorageSnController extends BaseController  {
	
	@Autowired
	private IMaterialInsideService iMaterialInsideService;
	
	
	@PostMapping("list")
	public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, RawStorageSn rawStorageSn) {
		rawStorageSn.preQuery();
		Pager<RawStorageSn> result = iMaterialInsideService.findRawStorageSnByCondition(page, rows, rawStorageSn);
		return success(result);
	}
	
	/** 
	 * @Title whiteBlackListIn
	 * @Description 查询黑白料入库明细
	 * @author luoteng
	 * @date 2018年6月4日:下午4:36:31
	 * @param page
	 * @param rows
	 * @param rawStorageSn
	 * @return TransResult
	 */
	@PostMapping("whiteBlack/list/in")
	public TransResult whiteBlackListIn(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, RawStorageSn rawStorageSn) {
		rawStorageSn.preQuery();
		Pager<RawStorageSn> result = iMaterialInsideService.findRawStorageSnWhiteBlackIn(page, rows, rawStorageSn);
		return success(result);
	}
	
	/** 
	 * @Title whiteBlackListOut
	 * @Description 查询黑白料出库明细
	 * @author luoteng
	 * @date 2018年6月4日:下午4:36:31
	 * @param page
	 * @param rows
	 * @param rawStorageSn
	 * @return TransResult
	 */
	@PostMapping("whiteBlack/list/out")
	public TransResult whiteBlackListOut(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, RawStorageSn rawStorageSn) {
		rawStorageSn.preQuery();
		Pager<RawStorageSn> result = iMaterialInsideService.findRawStorageSnWhiteBlackOut(page, rows, rawStorageSn);
		return success(result);
	}
	
	
	@GetMapping("r/detail")
	public TransResult findRawStorageSnByCode(RawStorageSn rawStorageSn) {
		rawStorageSn.preQuery();
		List<RawStorageSn> list = iMaterialInsideService.findRawStorageSnByCode(rawStorageSn);
		return success(list);
	}
	
	@PostMapping("r/detailPost")
	public TransResult postRawStorageSnByCode(RawStorageSn rawStorageSn) {
		rawStorageSn.preQuery();
		List<RawStorageSn> list = iMaterialInsideService.findRawStorageSnByCode(rawStorageSn);
		return success(list);
	}
	

	@PostMapping("uplist")
	public TransResult updateList(@RequestBody List<RawStorageSn> list) {
		TransResult reuslt = new TransResult();
		String currentUserName = getLoginName();
		RawLockLog rawLockLog = new RawLockLog();
		rawLockLog.setLockBy(currentUserName);
		Resp resp = new Resp();
		try {
		 resp = iMaterialInsideService.updateRawStorageSnStatus(list,rawLockLog);
		} catch(RuntimeException e){
			reuslt.setCode("0");
			System.out.print(e.getMessage());
			reuslt.setMsg(e.getMessage());
			return reuslt;
		}
		return fromResp(resp);
	}
	
	/** 
	 * @Title editRawStorageSnIn
	 * @Description 原材料入库明细 新增 修改
	 * @author luoteng
	 * @date 2018年6月4日:下午1:45:10
	 * @param rawStorageSn
	 * @return TransResult
	 */
	@PostMapping("in/edit")
	public TransResult editRawStorageSnIn(RawStorageSn rawStorageSn) {
		Date now = new Date();
		if(rawStorageSn.getStorageSnId() == null || "".equals(rawStorageSn.getStorageSnId())){
			rawStorageSn.setActiveFlag("1");
			rawStorageSn.setBarcodeStatus("0");
//			货主（0：本公司 1：外公司）
			rawStorageSn.setUserDefined3("0");
			rawStorageSn.setCreateBy(getLoginName());
			rawStorageSn.setGmtCreate(now);
			iMaterialInsideService.createRawStorageSnIn(rawStorageSn);
		}else{
			rawStorageSn.setLastModifiedBy(getLoginName());
			rawStorageSn.setGmtModified(now);
			iMaterialInsideService.updateRawStorageSn(rawStorageSn);
		}
		return success();
	}
	
	/** 
	 * @Title editRawStorageSnOut
	 * @Description 原材料出库明细 新增 修改
	 * @author luoteng
	 * @date 2018年6月4日:下午1:45:10
	 * @param rawStorageSn
	 * @return TransResult
	 */
	@PostMapping("out/edit")
	public TransResult editRawStorageSnOut(RawStorageSn rawStorageSn) {
		TransResult result = new TransResult();
		Date now = new Date();
		if(rawStorageSn.getStorageSnId() == null || "".equals(rawStorageSn.getStorageSnId())){
			rawStorageSn.setActiveFlag("1");
			rawStorageSn.setBarcodeStatus("0");
//			货主（0：本公司 1：外公司）
			rawStorageSn.setUserDefined3("0");
			rawStorageSn.setCreateBy(getLoginName());
			rawStorageSn.setGmtCreate(now);
			if(rawStorageSn.getAmount() != null){
				rawStorageSn.setAmount(rawStorageSn.getAmount().negate());
			}else{
				result.setCode(Constant.FAIL);
				result.setMsg("请输入出库数量！");
				return result;
			}
			Resp resp = iMaterialInsideService.createRawStorageSnOut(rawStorageSn);
			result.setCode(resp.getCode());
			result.setMsg(resp.getMsg());
			return result;
		}else{
			rawStorageSn.setLastModifiedBy(getLoginName());
			rawStorageSn.setGmtModified(now);
			if(rawStorageSn.getAmount() != null){
				rawStorageSn.setAmount(rawStorageSn.getAmount().negate());
			}
			Resp resp = iMaterialInsideService.updateRawStorageSnOut(rawStorageSn);
			result.setCode(resp.getCode());
			result.setMsg(resp.getMsg());
			return result;
		}
	}
	
	/** 
	 * @Title getRawStorageSn
	 * @Description 根据ID获取原材料入库信息
	 * @author luoteng
	 * @date 2018年6月4日:下午1:48:49
	 * @param id
	 * @return TransResult
	 */
	@GetMapping("get/{id}")
	public TransResult getRawStorageSn(@PathVariable("id")String id) {
		RawStorageSn rtnOb = new RawStorageSn();
		if(id != null && !"".equals(id)){
			RawStorageSn ob = new RawStorageSn();
			ob.setStorageSnId(Integer.parseInt(id));
			List<RawStorageSn> list = iMaterialInsideService.findRawStorageSnAbs(ob);
			if(list != null && !list.isEmpty()){
				rtnOb = list.get(0);
			}
		}
		return success(rtnOb);
	}
	
	/** 
	 * @Title deleteRawStorageSn
	 * @Description 原材料入库明细 新增 修改
	 * @author luoteng
	 * @date 2018年6月4日:下午1:45:10
	 * @param rawStorageSn
	 * @return TransResult
	 */
	@PostMapping("d")
	public TransResult deleteRawStorageSn(RawStorageSn rawStorageSn) {
		Date now = new Date();
//		有效标志 1-有效 0-无效
		rawStorageSn.setActiveFlag("0");
		rawStorageSn.setLastModifiedBy(getLoginName());
		rawStorageSn.setGmtModified(now);
		iMaterialInsideService.updateRawStorageSn(rawStorageSn);
		return success();
	}

	/**
	 * @wzhg 1865
	 * @param id
	 * @return
     */
	@GetMapping("findById/{id}")
	public TransResult findById(@PathVariable("id")String id){
		RawStorageSn rtnOb = new RawStorageSn();
		if(id != null && !"".equals(id)){
			RawStorageSn ob = new RawStorageSn();
			ob.setStorageSnId(Integer.parseInt(id));
			rtnOb = iMaterialInsideService.findById(ob);
		}
		return success(rtnOb);
	}

	/**
	 * 根据id修改合同号(可以置为空可以修改为其他值)
	 * @wzhg 1865
	 * @update jzh
	 * @return
	 */
	@PostMapping("updateById")
	public TransResult updateById(RawStorageSn rawStorageSn){
		TransResult result = new TransResult();
		result.setCode(Constant.SUCCESS);
		Date now = new Date();
		rawStorageSn.setLastModifiedBy(getLoginName());
		rawStorageSn.setGmtModified(now);
		try{
			Resp resp = iMaterialInsideService.updateById(rawStorageSn);
			if(resp != null){
				result.setCode(resp.getCode());
				result.setMsg(resp.getMsg());
			}
		}catch(RuntimeException e){
			result.setCode(Constant.FAIL);
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	/** 
	* @Title: updateBySelect 
	* @Description: updateBySelect根据id修改
	* @author jzh 
	* @throws 
	*/
	@PostMapping("updateBySelect")
	public TransResult updateBySelect(RawStorageSn rawStorageSn){
		Date now = new Date();
		rawStorageSn.setLastModifiedBy(getLoginName());
		rawStorageSn.setGmtModified(now);
		iMaterialInsideService.updateBySelect(rawStorageSn);
		return success();
	}
	
	/** 
	* @Title: updatePE 
	* @Description: 修改pe管长度，（判断不能为小数，不能已出库） 
	* @author jzh 
	* @throws 
	*/
	@PostMapping("updatePE")
	public TransResult updatePE(RawStorageSn rawStorageSn){
		TransResult result = new TransResult();
		rawStorageSn.preQuery();
		RawStorageSn tmp = new RawStorageSn();
		tmp.setStorageSnId(rawStorageSn.getStorageSnId());
		List<RawStorageSn> list = iMaterialInsideService.findRawStorageSnByCode(tmp);
		if(list.size()!= 0){
			if(list.get(0).getBarcodeStatus().equals("2")){
				result.setCode("0");
				result.setMsg("条码已出库不能修改数量");
				return result;				
			}
			BigDecimal b = list.get(0).getAmount();
			if(new BigDecimal(b.intValue()).compareTo(b)!=0){
				
				result.setCode("0");
				result.setMsg("只能修改长度为整数PE管");
				return result;				
			}
		}else{
			result.setCode("0");
			result.setMsg("条码不存在");
			return result;	
		}
		
		Date now = new Date();
		rawStorageSn.setLastModifiedBy(getLoginName());
		rawStorageSn.setGmtModified(now);
		iMaterialInsideService.updateBySelect(rawStorageSn);
		return success();
	}
	
	
	/** 
	* @Title: contactUpdate 
	* @Description: 合同号更新
	* @param @param rawStorageSn
	* @param @return  参数说明 
	* @return TransResult    返回类型
	* @author luoteng 
	* @date 2018年8月14日 上午9:52:53 
	* @throws 
	*/
	@PostMapping("contact/update")
	public TransResult contactUpdate(RawStorageSn rawStorageSn){
		TransResult result = new TransResult();
		//将登陆人暂存在LastModifiedBy
		String LoginName = getLoginName();
		rawStorageSn.setLastModifiedBy(LoginName);
		result.setCode(Constant.SUCCESS);
		try{
			Resp resp = iMaterialInsideService.batchUpdateContact(rawStorageSn);
			if(resp != null){
				result.setCode(resp.getCode());
				result.setMsg(resp.getMsg());
			}
		}catch(RuntimeException e){
			result.setCode(Constant.FAIL);
			result.setMsg(e.getMessage());
		}
		
		return result;
	}
}
